class MpdCallbacksWorker < BackgrounDRb::MetaWorker
  attr_accessor :mpd
  set_worker_name :mpd_callbacks_worker
  
  def create(args = nil)
    # this method is called, when worker is loaded for the first time
    self.mpd = MPD.new(Jukebox.mpd_config[:host], Jukebox.mpd_config[:port])
    
    #State has changes
    self.mpd.register_callback(MpdCallbacks.method('state_callback'), MPD::STATE_CALLBACK)
    
    #When the "now playing" song changes
    self.mpd.register_callback(MpdCallbacks.method('new_current_song_callback'), MPD::CURRENT_SONG_CALLBACK)
    
    #When a song gets added to the (mpd) playlist
    self.mpd.register_callback(MpdCallbacks.method('playlist_callback'), MPD::PLAYLIST_CALLBACK)
    
    #When the length of the playlist changes (and on start)
    self.mpd.register_callback(MpdCallbacks.method('playlist_length_callback'), MPD::PLAYLIST_LENGTH_CALLBACK)
    
    #When the next song is played (end of song or 'next' button)
    self.mpd.register_callback(MpdCallbacks.method('next_song_callback'), MPD::NEXT_SONG_CALLBACK)
    
    #When the previous song is played ('previous' button)
    self.mpd.register_callback(MpdCallbacks.method('previous_song_callback'), MPD::PREVIOUS_SONG_CALLBACK)
    
    self.mpd.connect(true)
    #MpdCallbacks.sync() #And sync the playlist!
    catch_callbacks #Run directly...
  end
  
  def catch_callbacks
    register_status("running")
    
    while self.mpd.connected?
      #Do nothing...
      sleep 10
    end
    
    register_status("disconnected")
    #logger.info 'mpd disconnected! Problem!!!!'
    print "aaargghhhhh....\n\n"
    
  end
end

